﻿Namespace Saglik.Eczane
	Module Functions

		Function EczaneSource() As String
			Return "q@select eczkod as kod, eczadi as ad from ssdmerkez..eczgenel order by eczadi"
		End Function

		Function Liste(ByVal Process As Token) As Recordset
			Return Process.OpenSQL("select * from ssdmerkez..eczgenel order by eczadi")
		End Function

		Function Oku(ByVal Process As Token, eczkod As String) As Recordset
			Return Process.OpenSQL("select * from ssdmerkez..eczgenel where eczkod = @eczkod", eczkod)
		End Function

		Function ReceteListesi(ByVal Process As Token, ByVal yil As Integer, ByVal ay As Integer) As Recordset
			Q("select a.eczkod, rtrim(b.adi) + ' ' + rtrim(b.soyadi) as sadi, b.eczadi, a.faturano, a.receteno, a.istsekli, a.ilactopfiat, a.ssd10, a.sig20, a.ssdtopfiat, a.kdv")
			Q("from ssdmerkez..recete a inner join ssdmerkez..eczgenel b on a.eczkod = b.eczkod")
			Q("where a.yil = @yil and a.ay = @ay order by a.eczkod")
			Return Process.OpenSQL("*", yil, ay)
		End Function
		Function ReceteListesi(ByVal Process As Token, eczkod As String, ByVal yil As Integer, ByVal ay As Integer) As Recordset
			Q("select a.eczkod, a.kimlik, a.sno, a.hturu, a.kararno, a.durum as sdrm, b.eczadi, a.faturano, a.receteno, a.istsekli, a.ilactopfiat, a.ssd10, a.sig20, a.ssdtopfiat, a.kdv")
			Q("from ssdmerkez..recete a inner join ssdmerkez..eczgenel b on a.eczkod = b.eczkod")
			Q("where a.eczkod = @eczkod and a.yil = @yil and a.ay = @ay order by a.faturano")
			Return Process.OpenSQL("*", eczkod, yil, ay)
		End Function

		Function ReceteOku(ByVal Token As Token, receteno As String) As Recordset
			Return Token.OpenSQL("select * from ssdmerkez.dbo.recete where receteno = @receteno", receteno)
		End Function

		Function FaturaOku(ByVal Token As Token, faturano As String) As Recordset
			Return Token.OpenSQL("select * from ssdmerkez.dbo.recete where faturano = @faturano", faturano)
		End Function

		Function OdemeDonemleri() As String
			Return "q@select distinct yil*100+ay as kod, ltrim(yil)+'/'+right('0'+ltrim(ay),2) as ad from ssdmerkez..eczcek order by 1 desc"
		End Function

		Function OdemeIstatistigi(ByVal Process As Token, ByVal Donem1 As String, ByVal Donem2 As String) As Recordset
			Q("select durum,yasa,uyruk,bolge,ydurum,sum(miktar) as miktar,count(*) as adet from (")
			Q("select durum,case when sno>10000000 then 'SGY' else 'SSY' end as yasa,case when kimlik between '000000' and '399999' and len(kimlik) <= 6 then '1' else '2' end as uyruk,")
			Q("substring(eczkod,1,2) as bolge,ydurum,ssdtopfiat as miktar")
			Q("from ssdmerkez..recete where yil*100+ay between :donem1 and :donem2) t group by durum,yasa,uyruk,bolge,ydurum")
			Return Process.OpenSQL("*", Donem1, Donem2)
		End Function

		Function CekTarihi(ByVal Process As Token, ByVal Donem As String) As Date
			Return Process.OpenSQL("select cektar from ssdmerkez..eczcek  where yil * 100 + ay = :donem", Donem)(0)
		End Function

		Function Ara(ByVal Token As Token, ByVal eczkod As String) As Recordset
			Return Token.OpenSQL("select * from eczgenel where eczkod = @eczkod", eczkod)
		End Function

		Sub Kaydet(ByVal Token As Token, ByVal eczkod As String,
		 ByVal durum As String, ByVal eczadi As String, ByVal adi As String, ByVal soyadi As String,
		 ByVal kimlik As String, ByVal sozbastar As Date, ByVal sozbittar As Date,
		 ByVal adr As String, ByVal adrsemt As String, ByVal adrsehir As String, ByVal tel As String, ByVal email As String)
			Dim Rs As Recordset = Ara(Token, eczkod)
			If Rs.EOF Then Rs.AddNew()
			Rs.SetValues("durum,eczadi,adi,soyadi", durum, eczadi, adi, soyadi)
			Rs.SetValues("kimlik,sozbastar,sozbittar", kimlik, sozbastar, sozbittar)
			Rs.SetValues("adr,adrsemt,adrsehir,tel,email", adr, adrsemt, adrsehir, tel, email)
			Rs.Save()
		End Sub

		Function IlacListesi(Token As Token) As Recordset
			Q("select ilactkod as Kod,ilactadi as Ad from ssdmerkez..ilactisim order by ilactadi")
			Return Token.OpenSQL("*")
		End Function
	End Module
End Namespace
